<template>
    <div>
        <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区 -->
        <el-card>
            <!-- 搜索与添加区域 -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
                        <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 用户列表区域 -->
            <el-table :data="userList" border stripe> 
                <el-table-column type="index"></el-table-column>
                <el-table-column label="商品封面">
                    <template slot-scope="scope">
                        <el-image :src="scope.row.goodsCoverImg"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="商品名称" prop="goodsName"></el-table-column>
                <el-table-column label="商品简介" >
                    <template slot-scope="scope">
                        <span v-html="scope.row.goodsIntro"></span>
                    </template>
                </el-table-column>
                <el-table-column label="商品价格" prop="goodsPrice" ></el-table-column>
                <el-table-column label="状态" prop="delete">
                    <template slot-scope="scope" >
                        <el-switch v-model="scope.row.delete" @change="userStateChanged(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime"></el-table-column>
                <!-- 自定义操作 -->
                <el-table-column label="操作" width="180px">
                    <template slot-scope="scope" >
                        <!-- 修改按钮 -->
                        <el-button type="primary" icon="el-icon-edit"  @click="showEditDialog(scope.row.goodsId)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
                <!-- 修改用户对话框 -->
                <el-dialog
                    title="修改用户"
                    :visible.sync="editDialogVisible"
                    width="50%"
                    @close="editDialogClosed"
                    >
                    <el-form :model="editFrom" :rules="editFromRules" ref="editFromRef" label-width="70px" >
                        <el-form-item label="商品ID">
                            <el-input v-model="editFrom.goodsId" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="商品名称" prop="goodsName">
                            <el-input v-model="editFrom.goodsName"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="goodsPrice">
                            <el-input v-model="editFrom.goodsPrice" type="number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品简介" prop="goodsIntro">
                            <quill-editor v-model="editFrom.goodsIntro"/>
                        </el-form-item>
                    </el-form>
                      <span slot="footer" class="dialog-footer">
                        <el-button @click="editDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="editUserInfo">确 定</el-button>
                      </span>
                </el-dialog>
        </el-card>
    </div>
</template>
<script>
export default {
    data() {
      return {
          //获取用户列表的参数对象
          queryInfo: {
              query: '',
              //当前页数
              pagenum: 1,
              //当前每页显示多少数据
              pagesize: 10
          },
          userList: [],
          total: 0,
          editDialogVisible: false,
          //查询到的用户信息对象
          editFrom: {},
          editFromRules: {
              goodsName: [
                  { required: true, message: '请输入商品名称', trigger: 'blur'}
              ],
              goodsPrice: [
                  { required: true, message: '请输入商品价格', trigger: 'blur'}
              ],
              goodsIntro: [
                  { required: true, message: '请输入商品介绍', trigger: 'blur'}
              ]
          },
          //控制分配角色的显示与隐藏
          setRoleDialogVisible: false,
          //需要被分配角色的用户信息
          userInfo: {},
          //所有角色的数据列表
          rolesList: {},
          //已选中的角色
          selectedRoleId: ''
      }   
    },
    created() {
        this.getUserList()
    },
    methods: {
        async getUserList() {
            const {data : res} = await this.$http.post('/api/goods/getAllGoods',this.queryInfo)
            if(res.code !==200) {
                return this.$message.error('获取商品列表失败！')
            }
            this.userList = res.data.records
            this.total = res.data.total
            console.log(res)    
        },
        //监听pagesize改变event
        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize
            this.getUserList()
        },
        //监听页码值改变事件
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage
            this.getUserList()
        },
        //监听switch开关改变
        async userStateChanged(userInfo) {
            const {data: res} = await this.$http.put('/api/goods/deleteGoods',userInfo)
            if (res.code !== 200) {
                userInfo.delete = !userInfo.delete
                return this.$message.error('更新商品状态失败')
            }
            this.$message.success('更新商品状态成功')
        },
        //监听添加用户对话框的关闭时间
        addDialogClosed() {
            this.$refs.addFormRef.resetFields()
        },
        //点击按钮添加新用户
        addUser() {
            this.$refs.addFormRef.validate(async valid =>{
                if (!valid) return
                //发起添加用户请求
                const {data: res} = await this.$http.put('/api/user/addUser',this.addForm)
                if(res.code !==201) { return this.$message.error(res.msg)}
                this.$message.success(res.msg)
                this.addDialogVisible = false
                //重新获取用户列表数据
                this.getUserList()
            })
        },
        //修改用户信息
        async showEditDialog(id) {
            const {data: res} = await this.$http.post('/api/goods/getOneGoods',{goodsId:id})
            if (res.code !==200) {
                return this.$message.error('查询商品数据失败')
            }
            console.log(res)
            this.editFrom = res.data
            this.editDialogVisible = true
        },
        //监听用户修改对话框的关闭时间
        editDialogClosed() {
            this.$refs.editFromRef.resetFields()
        },
        //修改用户信息并提交
        editUserInfo() {
            this.$refs.editFromRef.validate(async valid =>{
                if (!valid) {
                    return
                }
                //发起表单修改
                const {data: res} = await this.$http.put('/api/goods/updateGoods',this.editFrom)
                if (res.code !== 200) {
                    return this.$message.error(res.msg)
                }
                //关闭对话框
                this.editDialogVisible = false
                //刷新数据列表
                this.getUserList()
                //提示修改成功
                return this.$message.success(res.msg)
            })
        },
        goAddPage() {
            this.$router.push('/goods/add')
        },
    }
}
</script>

<style lang="less" scoped>
.el-dialog {
    width: 60%;
}
</style>